<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of model_transaksi
 *
 * @author Hadi
 */
class model_transaksi_penyewaan extends Default_My_Model {

    function __construct() {
        parent::__construct();
    }

    public function getPrefixPerjanjianNo() {
        $last_id =  $this->get_scalar("SELECT max(id_perjanjian) as id FROM data_perjanjian", "id");
        $number = $last_id+1;
        $prefix = str_pad($number, 5,0,STR_PAD_LEFT);
        return $prefix;
    }    

    function getPerjanjianData($whr, $offset = 0, $length= 30) {
        $sql = "SELECT i.*, c.*, b.nama_bank, b.nomor_rekening, b.cabang, v.nama_vessel,v.keterangan_vessel
                FROM data_perjanjian i                
                LEFT JOIN master_customer c ON i.id_customer = c.id_customer
                LEFT JOIN master_vessel v ON i.id_vessel = v.id_vessel
                LEFT JOIN master_bank b ON b.id_bank= i.id_bank
                $whr LIMIT $length OFFSET $offset";
        return $this->db->query($sql);
    }

    function deletePerjanjian($kode) {
        $this->db->delete("data_perjanjian", "id_perjanjian = '$kode'");
    }

    function insertCustomer($data) {
        $id = $this->namaPelangganExist($data["nama_customer"]);
        if ($id == "") {
            $kontak = explode("/", $data["kontak_komplit"]);
            $alamat = explode("\n", $data["alamat_komplit"]);
            $set = array(
                    "nama_customer" => $data["nama_customer"],
                    "alamat_detail_customer" => $data["alamat_detail_customer"],
                    "attendance" => $data["attendance"] ? $data["attendance"] : "NULL",
                    "telepon_customer" => $kontak[0],
                    "fax_customer" => $kontak[1],
                    "email_customer" => $kontak[2],
                    "alamat_customer" => $alamat[0],
                    "kota_customer" => $alamat[1],
                    "kode_pos_customer" => $alamat[2]
            );            
            $this->db->insert("master_customer", $set);
            return $this->db->insert_id();
        }
        return $id;
    }
    
    function insertVessel($data) {
        $set = array(
                "nama_vessel" => $data["nama_vessel"]
        );
        $this->db->insert("master_vessel", $set);
        return $this->db->insert_id();
    }

    function insertPort($data) {
        $set = array(
                "nama_port" => $data["nama_port"]
        );
        $this->db->insert("master_port", $set);
        return $this->db->insert_id();
    }

    function insertUpdatePerjanjian($data, $id_perjanjian = "") {
        $set = array(
                    "subject" => $data["subject"],
                    "id_vessel" => $data["id_vessel"],
                    "nomor_perjanjian" => $data["nomor_perjanjian"],
                    "tanggal_perjanjian" => $data["tanggal_perjanjian"],
                    "using_dollar_currency" => $data["using_dollar_currency"],
                    "tambahkan_ppn" => $data["tambahkan_ppn"],
                    "penerbit_perjanjian" => $data["penerbit_perjanjian"],
                    "approved_by" => $data["approved_by"],
                    "diskon" => $data["diskon"],
                    "kesediaan_muat" => $data["kesediaan_muat"],
                    "jenis_muatan"=> $data["jenis_muatan"],
                    "kondisi_kontrak"=> $data["kondisi_kontrak"],
                    "pelabuhan_muat"=> $data["pelabuhan_muat"],
                    "pelabuhan_bongkar"=> $data["pelabuhan_bongkar"],
                    "jumlah_hari"=> $data["jumlah_hari"],
                    "jumlah_muatan"=> $data["jumlah_muatan"],
                    "pengirim_barang"=> $data["pengirim_barang"],
                    "penerima_barang"=> $data["penerima_barang"],
                    "asuransi_barang"=> $data["asuransi_barang"],
                    "asuransi_kapal"=> $data["asuransi_kapal"],
                    "uang_tambang"=> $data["uang_tambang"],
                    "keagenan_kapal" => $data["keagenan_kapal"],
                    "kondisi_uang_tambang"=> $data["kondisi_uang_tambang"],
                    "cara_pembayaran"=> $data["cara_pembayaran"],
                    "denda_keterlambatan"=> $data["denda_keterlambatan"],
                    "remarks"=> $data["remarks"],
                    "id_bank"=> $data["id_bank"],
                    "id_customer"=> $data["id_customer"],
                    "submitter"=> $data["submitter"],
                    "date_submit" => $data["date_submit"]
        );
        if ($id_perjanjian == ""){
            $this->db->insert("data_perjanjian", $set);
            return $this->db->insert_id();
        } else {
            $this->db->update("data_perjanjian", $set, "id_perjanjian = '$id_perjanjian'");
        }
    }

    function namaPelangganExist($nama_pelanggan) {
        $id_customer = $this->get_scalar("SELECT id_customer FROM master_customer WHERE nama_customer = '".  addslashes($nama_pelanggan)."'", "id_customer");
        return $id_customer;
    }    
}

?>
